import {UserWaitApi} from "@/api/user_api";
import {resetLongToStr} from "@/utils/biz/common/BizUtils";
import {Observer} from "@/utils/observer/Observer";

/**
 * 候诊屏 - 呼叫记录 全量呼叫记录查询
 *
 * 典型模板：抽血中心、候诊-呼叫患者 等类似模板
 */
class WaitPatientLogObserverBiz extends Observer{
  constructor(self, deviceInfo = {}, item) {
    super();
    // 外部组件
    this.catcheInfo = self;
    // 设备信息
    this.deviceInfo = deviceInfo;
    // 当前组件信息
    this.currentInfo = item || {};
  }
  /**
   * 消息监听入口
   * @param bizType
   * @param msgContent
   */
  async update(bizType, msgContent) {
    let self = this.catcheInfo;
    try {
      switch (bizType) {
        case "refreshMsg":
          await this.refreshMsg(msgContent);
          break;
        case "openMsgWin":
          await this.openMsgWin(msgContent);
          break;
        case "closeMsgWin":
          await this.closeMsgWin(msgContent);
          break;
      }
    } catch (e) {
      console.error("消息数据加载失败：" + msgContent)
      self.handleException("sdkBizServices", "安卓消息消费异常 -- " + bizType + " -- >", JSON.stringify(e.stack));
    }
  }

 async init() {
   await this.getPatientLog()
 }

  /**
   * 刷新消息
   * @param str
   * @returns {Promise<void>}
   */
  async refreshMsg(str) {
    await this.getPatientLog();
  }
  /**
   * 获取呼叫记录
   */
  async getPatientLog() {
    let self = this.catcheInfo;
    try {
      const {data,code} = await UserWaitApi.getPatientLog({
        statusList: ["CALL_NEXT","CALL_SELECT"],
        registerObjIdList: [],
        pageSize: self.waitingScreenWaitingData.pageSize,
        pageNum: 1,
        orderByColumn: "operation_time",
        isAsc:'',// 排序的方向desc或者asc
        terminalId: self.deviceInfo.screenId,
        terminalType: self.deviceInfo.screenType,
        businessType: self.deviceInfo.businessType,
        tenantId: self.deviceInfo.hospitalId
      })
      if(code === 200){
        // 给组件发送数据
        this.waitingSetDataList(data.rows || []);
      }else{
        // 给组件发送数据
        this.waitingSetDataList([]);
      }
    }catch (e) {
      self.handleWriteLog('sdkServices',  "MtUserGetPatientLog --> 获取呼叫记录错误"+ JSON.stringify(e.stack));
    }
  }

  /**
   * 给组件写数据
   * @param data
   */
  waitingSetDataList(data){
    let self = this.catcheInfo;
    try {
      const formRef = self.$refs['vFormRef'];
      if (formRef) {
        formRef.executeChildMethod(this.currentInfo?.id, 'patientListInfo', data);
      }
    }catch (e) {
      self.handleException('sdkBizServices', '内部组件异常 --呼叫记录设置-- waitingSetDataList -- >', JSON.stringify(e.stack));
    }
  }

  /**
   * 打开弹窗
   * @param str
   */
  async openMsgWin(str) {
    const obj = JSON.parse(resetLongToStr(str));
    // debugger
    let self = this.catcheInfo;
    // 弹窗需要原始数据
    self.waitingScreenMsgWinData.data = obj;
    self.waitingScreenMsgWinData.deptName = obj.msgContent.deptName;
    self.waitingScreenMsgWinData.treatmentRoomName = obj.msgContent.treatmentRoomName;
    // 叫号器名称
    self.waitingScreenMsgWinData.workStationName = obj.msgContent.terminalName;
    self.waitingScreenMsgWinData.deptShortName = obj.msgContent.deptShortName;
    self.waitingScreenMsgWinData.treatmentRoomShortName = obj.msgContent.treatmentRoomShortName;
    self.waitingScreenMsgWinData.registerObjName = obj.msgContent.treatingPatient?obj.msgContent.treatingPatient.registerObjName : '';

    let treatingPatientList = obj.msgContent.treatingPatientList;
    if (treatingPatientList) {
      self.waitingScreenMsgWinData.currPatient = treatingPatientList || [];
    }else{
      self.waitingScreenMsgWinData.currPatient = obj.msgContent.treatingPatient?[obj.msgContent.treatingPatient] : [];
    }

    self.waitingScreenMsgWinData.waitPatient = obj.msgContent.waitingPatientList;
    self.waitingScreenMsgWinData.waitingAlert = obj.msgContent.waitingAlert;
    self.waitingScreenMsgWinData.treatingAlert = obj.msgContent.treatingAlert;
    self.waitingScreenMsgWinData.waitingVoiceContent = obj.msgContent.waitingVoiceContent;
    self.waitingScreenMsgWinData.treatingVoicePlayUrl = obj.msgContent.treatingVoicePlayUrl;
    // 正在就诊的控制文字
    self.waitingScreenMsgWinData.treatingVoiceContentConfig = obj.msgContent.treatingVoiceContentConfig;
    // 候诊语音播报的文案
    self.waitingScreenMsgWinData.waitingVoiceContent = obj.msgContent.waitingVoiceContent;
    let info = obj.msgContent.treatingVoiceContentConfig;
    if (!!info) {
      info.forEach((item) => {
        let key = item.type;
        let boolean = key === 'orderNo' || key === 'patientName' || key === 'treatmentRoomName' || key === 'deptName' || key === 'workerName';
        if (boolean) {
          Reflect.set(item, 'name', obj.msgContent.treatingPatient[key]);
          // 重置外层的科室或者诊室
          if (key === 'treatmentRoomName') {
            Reflect.set(item, 'name', obj.msgContent.treatmentRoomName);
          }
          if (key === 'deptName') {
            Reflect.set(item, 'name', obj.msgContent.deptName);
          }
          if (key === 'workerName') {
            Reflect.set(item, 'name', obj.msgContent.workerName);
          }
        }
      });
      //  就诊语音的配置数值信息
      self.waitingScreenMsgWinData.currPatientInfo = info;
    }

    // 如果不展示候诊
    if (!obj.msgContent.waitingAlert) {
      self.waitingScreenMsgWinData.waitPatient = [];
    }
    // 配置是否展示就诊弹窗/候诊弹窗
    self.hasCallShow = !!(obj.msgContent.treatingAlert || obj.msgContent.waitingAlert);
  }
  /**
   * 关闭弹窗
   */
  closeMsgWin() {
    let self = this.catcheInfo;
    self.hasCallShow = false;
  }


}

export {WaitPatientLogObserverBiz};
